import React, { Component } from 'react';
import Body from '../myComponents/body';
import Search from '../myComponents/search'
import CreateNotice from '../myComponents/createNotice'
import './index.scss';
import fetch from '../../util/fetch'
import BtnAlert from "../myComponents/btnAlert";
import { Checkbox, message, Pagination, Select } from "antd";
const {Option} = Select;
class IframePage extends Component {
    constructor(props){
        super(props);
        this.state = {
            currentKind: "publish",
            noRead: false,
            listInfo: {
                total: 0,
                unReadNum: 0
            },
            alertInfo: {},
            currentIndex: 1,
            noticeList: [],
            selectList: [],
            createVisible: false,
            editNoticeId: "",
            hadNoRead: false,
            keyword: "",
            status: "",
            kind: ""
        };
    }
    componentWillMount(){
        this.getList("", "", "")
        sessionStorage.setItem('editor', 'editor');
        window.addEventListener("message", (e) => {
            if(this.IsJSON(e.data) && JSON.parse(e.data).operateType){
                this.getList(this.state.keyword);
            }
        })
    }
    IsJSON = (str) => {
        let result = false;
        try {
            let obj = JSON.parse(str);
            result = true;
        } catch {
            result = false;
        }
        return result;
    }
    //获取当前类型通知列表
    getList = (value, count = "", countSign) => {
        let {
            currentIndex,
            status,
        } = this.state;
        let params = {
            title: value,
            // userId: sessionStorage.getItem("userId"),
            // status,
            sortType: count,
            eduId: sessionStorage.getItem("eduId"),
        }
        fetch.get({
            url: window.BasicProxy + `api/notice/list/page/${currentIndex}/10`,
            params
        })
        .then(res=>res.json())
        .then((result)=>{
            if(result.code == 200 && result.data){
                result.data.forEach(item => {
                    item.readNum = item.readCount
                    item.unReadNum = item.unReadCount
                })
                this.setState({
                    noticeList: Array.isArray(result.data)? result.data: [],
                    listInfo: {
                        total: result.total
                    }
                })
            }
        })
    }
    searchList = (value, count, countSign) => {
        this.setState({
            currentIndex: 1,
            keyword: value === -1? this.state.keyword: value
        }, ()=>{
            this.getList(this.state.keyword, count, countSign);
        })
    }
    //设置弹窗提示信息
    setAlert = (data) => {
        this.setState({
            alertInfo: data
        })
    }
    changeCreate = (value, id) => {
        this.setState({
            createVisible: value,
            editNoticeId: id? id: ""
        })
    }
    showCreate = () => {
        this.setState({
            createVisible: true,
            editNoticeId: ""
        })
    }
    changeUrgent = (value, id) => {
        this.setState({
            urgentVisible: value,
            editNoticeId: id? id: ""
        })
    }
    render() {
        let {
            currentKind,
            listInfo,
            noticeList,
            currentIndex,
            selectList,
            alertInfo,
            createVisible,
            editNoticeId,
        } = this.state;
        return (
            <div className="Main">
                <div className="Main_body">
                  <div className="Main_div">
                      <div className="option_container">
                          <div className="option_left">
                              <span className="main_text">共<span style={{color: "#333"}}>{listInfo.total}</span>条通知</span>
                          </div>
                          <div className="option_right">
                              <Search searchMethod={this.searchList} />
                              <div className="blue_btn" onClick={()=>this.showCreate()}><i className="addlogo"></i>发布通知公告</div>
                          </div>
                      </div>
                      <Body
                          dataList={noticeList} 
                          keyword={""} 
                          setAlert={this.setAlert}
                          changeCreate={this.changeCreate}
                          changeUrgent={this.changeUrgent}
                          selectList={selectList}
                          getList={this.searchList}
                          iframe={true}
                      ></Body>
                      <div className="table_operate">
                          <Pagination
                          className="table_pagination"
                          size="small"
                          total={Number(listInfo.total)}
                          current={currentIndex}
                          pageSize={10}
                          // showQuickJumper={false}
                          showSizeChanger={false}
                          hideOnSinglePage
                          onChange={(pageIndex)=>{
                              this.setState({
                                  currentIndex: pageIndex,
                                  selectList: []
                              }, ()=>{
                                  this.getList("");
                              })
                          }} />
                      </div>
                  </div>
                </div>

                {
                    createVisible?
                    <CreateNotice
                    visible={createVisible}
                    getList={() => this.searchList(this.state.keyword)}
                    noticeId={editNoticeId}
                    changeVisible={(value) => {this.changeCreate(value)}}
                    />:
                    ""
                }
                
                <BtnAlert 
                visible={alertInfo.visible} 
                title={alertInfo.title} 
                data={alertInfo.data}
                smallTitle={alertInfo.smallTitle}
                onOk={alertInfo.onOk}
                onCancel={alertInfo.onCancel} />

            </div>
        )
    }
}

export default IframePage;